home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / io / StringWriter.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  3.3 KB  |  141 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)StringWriter.java    1.15 98/09/30
  3.  *
  4.  * Copyright 1996-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17.  
  18. /**
  19.  * A character stream that collects its output in a string buffer, which can
  20.  * then be used to construct a string.
  21.  *
  22.  * @version     1.15, 98/09/30
  23.  * @author    Mark Reinhold
  24.  * @since    JDK1.1
  25.  */
  26.  
  27. public class StringWriter extends Writer {
  28.  
  29.     private StringBuffer buf;
  30.  
  31.     /**
  32.      * Flag indicating whether the stream has been closed.
  33.      */
  34.     private boolean isClosed = false;
  35.  
  36.     /** Check to make sure that the stream has not been closed */
  37.     private void ensureOpen() {
  38.         /* This method does nothing for now.  Once we add throws clauses
  39.      * to the I/O methods in this class, it will throw an IOException
  40.      * if the stream has been closed.
  41.      */
  42.     }
  43.  
  44.     /**
  45.      * Create a new string writer, using the default initial string-buffer
  46.      * size.
  47.      */
  48.     public StringWriter() {
  49.     buf = new StringBuffer();
  50.     lock = buf;
  51.     }
  52.  
  53.     /**
  54.      * Create a new string writer, using the specified initial string-buffer
  55.      * size.
  56.      */
  57.     public StringWriter(int initialSize) {
  58.     if (initialSize < 0) {
  59.         throw new IllegalArgumentException("Negative buffer size");
  60.     }
  61.     buf = new StringBuffer(initialSize);
  62.     lock = buf;
  63.     }
  64.  
  65.     /**
  66.      * Write a single character.
  67.      */
  68.     public void write(int c) {
  69.     ensureOpen();
  70.     buf.append((char) c);
  71.     }
  72.  
  73.     /**
  74.      * Write a portion of an array of characters.
  75.      *
  76.      * @param  cbuf  Array of characters
  77.      * @param  off   Offset from which to start writing characters
  78.      * @param  len   Number of characters to write
  79.      */
  80.     public void write(char cbuf[], int off, int len) {
  81.     ensureOpen();
  82.         if ((off < 0) || (off > cbuf.length) || (len < 0) ||
  83.             ((off + len) > cbuf.length) || ((off + len) < 0)) {
  84.             throw new IndexOutOfBoundsException();
  85.         } else if (len == 0) {
  86.             return;
  87.         }
  88.         buf.append(cbuf, off, len);
  89.     }
  90.  
  91.     /**
  92.      * Write a string.
  93.      */
  94.     public void write(String str) {
  95.     ensureOpen();
  96.     buf.append(str);
  97.     }
  98.  
  99.     /**
  100.      * Write a portion of a string.
  101.      *
  102.      * @param  str  String to be written
  103.      * @param  off  Offset from which to start writing characters
  104.      * @param  len  Number of characters to write
  105.      */
  106.     public void write(String str, int off, int len)  {
  107.     ensureOpen();
  108.     buf.append(str.substring(off, off + len));
  109.     }
  110.  
  111.     /**
  112.      * Return the buffer's current value as a string.
  113.      */
  114.     public String toString() {
  115.     return buf.toString();
  116.     }
  117.  
  118.     /**
  119.      * Return the string buffer itself.
  120.      */
  121.     public StringBuffer getBuffer() {
  122.     return buf;
  123.     }
  124.  
  125.     /**
  126.      * Flush the stream.
  127.      */
  128.     public void flush() { 
  129.     ensureOpen();
  130.     }
  131.  
  132.     /**
  133.      * Close the stream.  This method does not release the buffer, since its
  134.      * contents might still be required.
  135.      */
  136.     public void close() throws IOException { 
  137.     isClosed = true;
  138.     }
  139.  
  140. }
  141.